Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Uncaught TypeError: workouts.map is not a function "react"

this is the JSON file that is coming from the server

 {workouts: Array(3)}
workouts: Array(3)
0: {_id: 'idnumber1', title: 'pullup', reps: 40, load: '20', createdAt: '2022-07-20T18:06:39.642Z', …}
1: {_id: 'idnumber2', title: 'situp', reps: 40, load: '20', createdAt: '2022-07-20T10:10:14.078Z', …}
2: {_id: 'idnumber3', title: 'pushup', reps: 10, load: '0', createdAt: '2022-07-19T14:33:58.678Z', …}
length: 3
[[Prototype]]: Array(0)
[[Prototype]]: Object

//this is the code I did in react

import { useEffect, useState } from "react";

const Home = () => {
  const [workouts, setworkouts] = useState(null);

  useEffect(() => {
    const fecthworkout = async () => {
      const response = await fetch("/api/workout");
      const json = await response.json();

      if (response.ok) {
        setworkouts(json);
      }
    };

    fecthworkout();
  }, []);

  useEffect(() => {
    console.log("hello");
  }, []);

  return (
    <div className="home">
      <div className="workouts">
 
        {workouts &&
          workouts.map((workouts) => (
           <p key={workouts._id}>{workouts.title}</p>
          ))}
      </div>
    </div>
  );
};

export default Home;

//can any tell me how can I stop the error?

I am stuck here I need help here

I am stuck here I need help here

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your usestate hook should by default be useState( [ ] ) and not useState(null)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!